package com.module.admin.controller;

import com.module.admin.Warpper.DictWarpper;
import com.module.common.factory.ConstantFactory;
import com.module.service.IDictService;
import com.module.common.BaseController;
import com.module.model.Dict;
import com.module.user.domain.BaseResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 字典控制器
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private String PREFIX = "/back/system/dict/";

    @Autowired
    private IDictService dictService;

    /**
     * 跳转到字典管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dict.btl";
    }

    /**
     * 跳转到添加字典
     */
    @RequestMapping("/dict_add")
    public String deptAdd() {
        return PREFIX + "dict_add.btl";
    }

    /**
     * 跳转到修改字典
     */
    @RequestMapping("/dict_edit/{dictId}")
    public String deptUpdate(@PathVariable Integer dictId, Model model) {
        Dict dict = dictService.selectById(dictId);
        model.addAttribute("dict", dict);
        List<Dict> subDicts = ConstantFactory.me().findInDict(dictId);
        model.addAttribute("subDicts", subDicts);
        return PREFIX + "dict_edit.btl";
    }

    /**
     * 新增字典
     *
     * @param dictValues 格式例如   "1:启用;2:禁用;3:冻结"
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public void add(String dictName, String dictValues, HttpServletResponse response) {
        this.dictService.addDict(dictName, dictValues);
        BaseResp<String> resp=new BaseResp<String>();
        sendSuccess(resp,"增加成功");
    }

    /**
     * 获取所有字典列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.dictService.list(condition);
        Object data=new DictWarpper(list).warp();
        return  data;
    }

    /**
     * 字典详情
     */
    @RequestMapping(value = "/detail/{dictId}")
    @ResponseBody
    public Object detail(@PathVariable("dictId") Integer dictId) {
        return dictService.selectById(dictId);
    }

    /**
     * 修改字典
     */

    @RequestMapping(value = "/update")
    @ResponseBody
    public BaseResp<String> update(Integer dictId, String dictName, String dictValues) {
        dictService.editDict(dictId, dictName, dictValues);
        BaseResp resp = new BaseResp();
        resp.setStatus(0);
        resp.setMessage("修改成功");
        return resp;
    }

    /**
     * 删除字典记录
     */

    @RequestMapping(value = "/delete")
    @ResponseBody
    public BaseResp<String> delete(@RequestParam Integer dictId) {
        this.dictService.delteDict(dictId);
        BaseResp resp = new BaseResp();
        resp.setStatus(0);
        resp.setMessage("删除成功");
        return resp;
    }

}
